/*******************************************************************************
 * 外语翻译平台(translate)
 * 
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 * 
 * @Version : 1.0 2011-1-9 下午03:14:58
 * 
 * release.js
 * 
 ******************************************************************************/

function step01Check() {
	var size = 0;
	var title = $("#title").val();
	size += addErrorMessage($("#title"), '请输入项目的标题', title.isEmpty());
	var from = $("#lang_from").val();
	var to = $("#lang_to").val();
	size += addErrorMessage($("#lang_to"), '原始语言与目标语言不能相同', from == to);
	var area = $("#area_direction").val();
	size += addErrorMessage($("#area_direction"), '请选择该项目所涉及的领域', area.isEmpty() || !area.isDigit());
	var cost = $("#cost").val();
	size += addErrorMessage($("#cost"), '请输入项目预算,只能为数字', cost.isEmpty() || !cost.isDigit());
	var period = parseInt($("#period").val());
	var desc = $("#desc").val();
	size += addErrorMessage($("#desc"), '请输入项目需求描述', desc.isEmpty());
	var file = $("#select_file").val();
	size += addErrorMessage($("#select_file"), '请选择需要翻译的项目文档', file.isEmpty());
	var trans_type = new Array();
	$(":radio[name='goodAt']").each(function (){
		if ($(this).attr('checked')) {
			trans_type.push($(this).next('label:eq(0)').text());
		}
	});
	size += addErrorMessage($(":checkbox[name='goodAt']:eq(0)"), '请选择翻译类型', trans_type.length == 0);
	
	if (!file.isEmpty()) {
		var suffix = file.substring(file.indexOf('.') + 1);
		suffix = suffix.toLowerCase();
		size += addErrorMessage($("#select_file"), '只能上传word格式的文档', suffix != 'doc');
	}
	
	var invoice = parseInt($(":radio:checked").val());
	
	var start_date = new Date();
	var end_date = new Date();
	end_date.setTime(start_date.getTime() + period * 24 * 60 * 60 * 1000);
	
	$("#title_txt").html(title);
	$("#trans_dire").html($("#lang_from option[value='"+from+"']").text() + "->" + $("#lang_to option[value='"+to+"']").text());
	$("#cost_txt").html('<b>￥：'+cost+'RMB</b>');
	$("#area_txt").html($("#choosearea").val());
	$("#invoice_txt").html(invoice == 1 ? '是':'否');
	$("#period_txt").html('<em>'+dateString(start_date) + '~' + dateString(end_date)+' (' + period+'天) </em>');
	$("#desc_txt").html(desc.replace('\r\n', '<br/>').replace('\n', '<br/>').replace('\r', '<br/>'));
	$("#doc_txt").html(file);
	$("#workDaysTxt").html($("#workdays").val()+'天');
	$("#trans_type").html(trans_type.join());
	
	var keys = new Array();
	keys.push($("#choosearea").val());
	keys.push($("#lang_from option[value='"+from+"']").text());
	keys.push($("#lang_to option[value='"+to+"']").text());
	keys.push(trans_type.join());
	$("#keywords").val(keys.join(','));
	
	return size == 0;
}

function dateString(date) {
	if (date == null) {
		return "";
	}
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	month = month < 10 ? '0' + month : month;
	var day = date.getDate();
	day = day < 10 ? '0' + day : day;
	return year + '-' + month + '-' + day;
}

function addErrorMessage(obj, msg, add) {
	var error_id = $(obj).attr('id') + "_error";
	var prev = $(obj).parent().parent().prev().first();

	if (!add) {
		if (prev.attr('id') == error_id) {
			prev.remove();
		}
		return 0;
	}

	if (prev.attr('id') == error_id) {
		$(prev).children('span').text(msg);
	} else {
		var messge = '<tr id="' + error_id
				+ '"><td></td><td colspan="2"><span style="color:red;">' + msg
				+ '</span></td></tr>';
		$(prev).after(messge);
	}
	return 1;
}